package com.orhon.smartcampus.modules.material.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.material.dao.CampusDao;
import com.orhon.smartcampus.modules.material.dao.SiteDao;
import com.orhon.smartcampus.modules.material.entity.Campus;
import com.orhon.smartcampus.modules.material.entity.Site;
import com.orhon.smartcampus.modules.material.gqlInputModel.GQLSiteInput;
import com.orhon.smartcampus.modules.material.inputMapper.SiteInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddSiteMutation implements DataFetcher {



    @Autowired
    SiteDao siteDao;

    @Autowired
    CampusDao campusDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 场地管理
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);

        GQLSiteInput input = SiteInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        Site site = null;

        if (input.getId() != null) {
            site = siteDao.get(Site.class, input.getId());
            site.setUpdatedAt(new Date());
        }
        else{
            site = new Site();
            site.setCreatedAt(new Date());
        }

        if(input.getCampus_id() != null) {
            site.setMaterialCampusByCampusId( campusDao.get(Campus.class, input.getCampus_id()));
        }
        if(input.getSite_use() != null){

            site.setSiteUse(input.getSite_use());
        }
        if(input.getArea() != null){

            site.setSiteArea(input.getArea());
        }
        if(input.getRight_of_use() != null){

            site.setSiteRightUse(input.getRight_of_use());
        }
        if(input.getTel() != null) {
            site.setSiteTel(input.getTel());
        }
        if(schoolId != null) {
            site.setBaseSchoolsBySchoolId( schoolsDao.get(Schools.class, schoolId));
        }
        if(input.getName() != null){

            site.setSiteName(JSON.toJSONString(input.getName()));
        }
        if(input.getManager() != null){

            site.setSiteManager(JSON.toJSONString(input.getManager()));
        }
        try{
            siteDao.saveOrUpdate(site);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

